<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <script src="X-admin/js/jquery.js"></script>
    <link rel="stylesheet" href="X-admin/css/xadmin.css">
    <link rel="stylesheet" href="X-admin/css/font.css">
    <script src="X-admin/lib/layui/layui.js"></script>
    <script src="X-admin/js/xadmin.js"></script>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">
            <div class="layui-form-item">
                <label for="L_orcode" class="layui-form-label">
                    <span class="x-red">*</span>订单号</label>
                <div class="layui-input-inline">
                    <input type="text" id="L_orcode" name="orcoe" required="" lay-verify="orcoe"
                           autocomplete="off" class="layui-input"></div>
            </div>
            <div class="layui-form-item">
                <label for="L_unitpr" class="layui-form-label">
                    <span class="x-red">*</span>工位单价</label>
                <div class="layui-input-inline">
                    <input type="text" id="L_unitpr" name="unitpr" required="" lay-verify="unitpr"
                           autocomplete="off" class="layui-input" onkeyup="sum(this);"></div>
            </div>

            <div class="layui-form-item">
                <label for="L_reduce" class="layui-form-label">
                    <span class="x-red">*</span>折扣</label>
                <div class="layui-input-inline">
                    <input type="text" id="L_reduce" name="reduce" required="" lay-verify="reduce"
                           autocomplete="off" class="layui-input" onkeyup="sum(this);"></div>
            </div>

            <div class="layui-form-item">
                <label for="L_oldTime" class="layui-form-label">
                    <span class="x-red">*</span>原租赁时长</label>
                <div class="layui-input-inline">
                    <input type="text" id="L_oldTime" name="oldTime" required="" lay-verify="oldTime"
                           autocomplete="off" class="layui-input"></div>
            </div>

            <div class="layui-form-item">
                <label for="L_newTime" class="layui-form-label">
                    <span class="x-red">*</span>新时长</label>
                <div class="layui-input-inline">
                    <input type="text" id="L_newTime" name="newTime" required="" lay-verify="newTime"
                           autocomplete="off" class="layui-input" onkeyup="sum(this);"></div>
            </div>

            <div class="layui-form-item">
                <label for="L_price" class="layui-form-label">
                    <span class="x-red">*</span>续租价格</label>
                <div class="layui-input-inline">
                    <input type="text" id="L_price" name="price" required="" lay-verify="price"
                           autocomplete="off" class="layui-input"></div>
            </div>

            <div class="layui-form-item" style="display: flex;justify-content: center">
                <button class="layui-btn" lay-filter="leaseRenewal" lay-submit="">提交</button>
            </div>
        </form>
    </div>
</div>
<script>
    var Address_Prefix = "http://localhost:9200/api-person";
    start();

    //给表单标签赋值，回显
    function start() {
        console.log(1)
        $("#L_orcode").val(sessionStorage.getItem("orcode"));
        $("#L_oldTime").val(sessionStorage.getItem("letime") + "小时");
        $("#L_unitpr").val(sessionStorage.getItem("unitpr") + "元/小时");
        if (sessionStorage.getItem("reduce") < 1) {
            $("#L_reduce").val(sessionStorage.getItem("reduce") * 10 + "折");
        } else {
            $("#L_reduce").val("无");
        }
    }

    //实时计算新的价格，部分取值使用了js的原生从input标签取值的方法
    function sum(obj) {
        let newTime = document.getElementById("L_newTime");
        let unpitr = document.getElementById("L_unitpr");
        if (newTime.value != null && newTime.value != "") {
            $("#L_price").val(parseInt(newTime.value) * parseInt(unpitr.value) * sessionStorage.getItem("reduce") + "元");
        }
    }

    layui.use('form', function (data) {
        var form = layui.form;
        form.on('submit(leaseRenewal)', function (data) {
                var newTime = document.getElementById("L_newTime");
            let feild = {
                letime: parseInt(newTime.value),
                reduce: sessionStorage.getItem("reduce"),
                unitpr: sessionStorage.getItem("unitpr"),
                orcode: sessionStorage.getItem("orcode"),
                userid: sessionStorage.getItem("userid")
            }
            $.ajax({
                url: Address_Prefix + "/order/leaseRenewal",
                data: feild,
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    if (data.code === 0) {
                        layer.msg("续租完成！", {time: 1000}, function () {
                            //关闭当前frame
                            xadmin.close();
                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        })
                    }
                }
            });
            return false;
        })
    })
</script>
</body>
</html>